package net.hn.hnms.biz.common.activiti.builder;


import net.hn.hnms.biz.common.activiti.params.TaskParam;
import net.hn.hnms.biz.common.activiti.result.ProcessTaskResult;
import net.hn.hnms.biz.common.activiti.service.ProcessApiService;

import java.util.Map;

/**
 * Created on 2022/8/5.
 *
 * @author ly
 * @desc: 流程任务执行器
 */
public class TaskExecutor {


    private TaskParam taskParam = new TaskParam();

    //流程id
    private String taskId;

    //执行人
    private String executor;

    //执行参数
    private Map<String, Object> variables;

    private ProcessApiService processApiService;


    private TaskExecutor() {
    }

    public TaskExecutor(ProcessApiService processApiService) {
        this.processApiService = processApiService;
    }

    /**
     * 创建流程参数查询器
     * @return TaskVariablesQuery 流程参数查询器
     */
    public TaskVariablesQuery createTaskVariablesQuery() {
        return new TaskVariablesQuery(this.processApiService);
    }

    public TaskExecutor taskId(String taskId) {
        taskParam.setTaskId(taskId);
        return this;
    }

    public TaskExecutor executor(String executor) {
        taskParam.setExecutor(executor);
        return this;
    }

    public TaskExecutor businessKey(String businessKey) {
        taskParam.setBusinessKey(businessKey);
        return this;
    }


    public TaskExecutor variables(Map<String, Object> variables) {
        taskParam.setVariables(variables);
        return this;
    }

    public TaskExecutor variable(String key, Object value) {
        taskParam.getVariables().put(key, value);
        return this;
    }

    /**
     * 根据传入的流程执行参数,执行流程任务
     * @return ProcessTaskResult 执行流程任务后,获得的下一个流程任务的信息
     */
    public ProcessTaskResult execute() {
        return processApiService.taskExecute(taskParam);
    }
}
